library(dplyr)
library(lfe)
library(texreg)

# Load cosponsorship data from House and Senate
house_data <- readRDS("D://Dropbox//Solidarity//Data//house_orig_cosponsor_network.rds") %>% mutate(chamber = "House", cs = n, sponsor = as.numeric(sponsor_icpsr_id), 
                                                                                                                   cosponsor = as.numeric(cosponsor_icpsr_id), congress = cong) %>%
  select(sponsor, cosponsor, congress, chamber, cs)
senate_data <- readRDS("D://Dropbox//Solidarity//Data//senate_orig_cosponsor_network.rds") %>% mutate(chamber = "Senate", cs = n, sponsor = as.numeric(sponsor_icpsr_id), 
                                                                                                                     cosponsor = as.numeric(cosponsor_icpsr_id), congress = cong) %>%
  select(sponsor, cosponsor, congress, chamber, cs)

# Concatenate to form CS Data
cs_data <- rbind(house_data, senate_data) %>% mutate(prev_cycle = (congress-117)*2 + 2020) %>% select(-congress) %>% distinct()

# Candidates have FEC IDs, so we need to translate those to ICPSR IDs
fec_map <- read.csv("D://Dropbox//Solidarity//Data//fec_icpsr_ids.csv") %>% select(cycle, icpsr_id, Cand_id) %>% distinct() %>% 
  filter(!is.na(as.numeric(icpsr_id))) %>% mutate(receiver_icpsr = as.numeric(icpsr_id), .keep = "unused")

# Load donation data and merge with CS data through the FEC Map
women_cop_data <- read.csv("D://Dropbox//Solidarity//Data//women_copartisan_donations.csv") %>% mutate(pair_id = paste0(receiver,year,matchid)) %>%
  inner_join(fec_map, by = c("receiver" = "Cand_id", "year" = "cycle")) %>% 
  inner_join(cs_data, by = c("giver" = "cosponsor", "receiver_icpsr" = "sponsor",  "year" = "prev_cycle", "chamber" = "chamber")) %>% mutate(in_cs = cs, .keep = "unused") %>%
  inner_join(cs_data, by = c("giver" = "sponsor", "receiver_icpsr" = "cosponsor",  "year" = "prev_cycle", "chamber" = "chamber")) %>% mutate(out_cs = cs, .keep = "unused")
  
women_amount_cop_data <- read.csv("D://Dropbox//Solidarity//Data//women_copartisan_donations_amount.csv") %>% mutate(pair_id = paste0(receiver,year,matchid)) %>%
  inner_join(fec_map, by = c("receiver" = "Cand_id", "year" = "cycle")) %>% 
  inner_join(cs_data, by = c("giver" = "cosponsor", "receiver_icpsr" = "sponsor",  "year" = "prev_cycle", "chamber" = "chamber")) %>% mutate(in_cs = cs, .keep = "unused") %>%
  inner_join(cs_data, by = c("giver" = "sponsor", "receiver_icpsr" = "cosponsor",  "year" = "prev_cycle", "chamber" = "chamber")) %>% mutate(out_cs = cs, .keep = "unused")


# Regressions for Table 7
women.in.fit <- felm(in_cs ~ gave + I(gender == "F") + num_other_recipients + same_state + I(race == cand_race) | pair_id | 0 | receiver_icpsr,
             data = women_cop_data)
women.in.amount.fit <- felm(in_cs ~ I(giving/1000) + I(gender == "F") + I(other_giving/1000) + same_state + I(race == cand_race) | pair_id | 0 | receiver_icpsr,
             data = women_amount_cop_data)

# Table 7
texreg(list(women.in.fit, women.in.amount.fit),
       custom.model.names = c("Gave?", "Amount"),
       custom.coef.names = c("Gve",
                             "Female",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Amount Given (thousands)",
                             "Total Donor Gave to Other Candidates"),
       caption = "Giving is Correlated with Being an Original Cosponsor",
       label = "tab:incs",
       booktabs = TRUE,    # For LaTeX styling
       dcolumn = TRUE,     # Align decimals if using dcolumn
       use.packages = FALSE, # Avoid extra package suggestions
       digits = 3
)

# Regressions for Table 8
women.out.fit <- felm(out_cs ~ gave + I(gender == "F") + num_other_recipients + same_state + I(race == cand_race) | pair_id | 0 | receiver_icpsr,
                      data = women_cop_data)
women.out.amount.fit <- felm(out_cs ~ I(giving/1000) + I(gender == "F") + I(other_giving/1000) + same_state + I(race == cand_race) | pair_id | 0 | receiver_icpsr,
                             data = women_amount_cop_data)

# Table 8
texreg(list(women.out.fit, women.out.amount.fit),
       custom.model.names = c("Gave?", "Amount"),
       custom.coef.names = c("Gve",
                             "Female",
                             "Other Campaigns Receiving from Donor",
                             "Same State as Recipient",
                             "Same Race as Recipient",
                             "Amount Given (thousands)",
                             "Total Donor Gave to Other Candidates"),
       caption = "Recipients Reciprocate Giving by Cosponsoring the Donor's Legislation",
       label = "tab:outcs",
       booktabs = TRUE,    # For LaTeX styling
       dcolumn = TRUE,     # Align decimals if using dcolumn
       use.packages = FALSE, # Avoid extra package suggestions
       digits = 3
)
